package com.back.controller;

import com.alibaba.fastjson.JSONObject;
import com.back.model.dto.user.*;
import com.back.model.entity.User;
import com.back.model.reslut.PageData;
import com.back.model.reslut.Result;
import com.back.model.vo.UserVO;
import com.back.service.UserService;
import com.back.utils.Redisutils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@RequestMapping("/user")
@Slf4j
public class LogInController {

    @Resource
    private UserService userService;


    @Resource
    private HttpSession httpSession;

    @Resource
    private Redisutils redisutils;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody @Validated UserDTO user) {
        return Result.success(userService.login(user), "登录成功");
    }


    /**
     * 注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) {

        return userService.register(registerDTO) ? Result.successMessage("注册成功") : Result.faile("注册失败");
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable @NotNull(message = "id不能为空") Integer id) {
        userService.removeById(id);
        return Result.success();
    }


    /**
     * 修改用户信息
     * @param userUpdatePageDTO
     * @return
     */
    @GetMapping("/updateUser")
    public Result updateUser(@PathVariable @Validated UserUpdatePageDTO userUpdatePageDTO) {
        User user = new User();
        BeanUtils.copyProperties(userUpdatePageDTO, user);
        userService.updateById(user);
        return Result.success();
    }





    /**
     * 分页查询用户信息
     * @param queryPageDTO
     * @return
     */
    @PostMapping("/pageUser")
    public Result<PageData<UserVO>> getPageUser(@RequestBody @Validated UserQueryPageDTO queryPageDTO) {
        PageData<UserVO> pageUser = userService.getPageUser(queryPageDTO);
        return Result.successData(pageUser);
    }

    /**
     * 修改密码
     * @param updatePasswordDTO
     * @return
     */
    @GetMapping("/updatePas")
    public Result updateUserPassword(@PathVariable @Validated UpdatePasswordDTO updatePasswordDTO) {
        return userService.updateUserPassword(updatePasswordDTO) ? Result.successMessage("修改密码成功") : Result.faile("修改密码失败");
    }

    @GetMapping("/test011")
    public Result test011() {
        System.out.println("中文乱码");
        log.info("中文乱码");
        return Result.success();
    }

    @GetMapping("/ty")
    public Result<Object> ty() throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","ty");
        return Result.successData(jsonObject);
    }

}
